<template>
  <view class="bd">
    <p class="banner" v-show="isPtUser">
      <span
        >推送开关<sup style="font-size: 10px"
          >*关闭后可以将所有用户的推送功能关闭</sup
        ></span
      >
      <span style="position: relative">
        <span class="tips">{{ isPtUserCode ? "开启" : "关" }}</span>
        <span class="tips t1" v-show="!isPtUserCode">关闭</span>
        <u-switch
          v-model="isPtUserCode"
          active-color="#18c7e1"
          @change="change"
        >
        </u-switch>
      </span>
    </p>
    <u-modal
      v-model="show"
      :content="content"
      :show-cancel-button="true"
      @cancel="cancel"
      @confirm="confirm"
    ></u-modal>
  </view>
</template>

<script>
export default {
  data() {
    return {
      show: false, //弹框
      isPtUser: true, //是否注册用户
      isPtUserCode: false, // 状态
      content: "是否关闭当前设备下所有用户的推送功能",
    };
  },
  onLoad() {
    // this.getUiStyle();
    this.getOpenCode();
  },
  onReady() {
    // this.getUiStyle();
    this.getOpenCode();
  },
  methods: {
    cancel() {
      this.isPtUserCode = false;
    },
    confirm() {
      let flag = this.isPtUserCode ? 1 : 0;
      let device = uni.getStorageSync("device", 1);
      let cid = uni.getStorageSync("cid");
      let userId = uni.getStorageSync("user").id;
      console.log(device);
      this.config
        .post("/uni/isOpenPush", {
          isPush: flag,
          ptType: device,
          cid,
          userId,
        })
        .then((res) => {
          console.log(res);
        });
    },
    getOpenCode() {
      let userId = uni.getStorageSync("user").id;
      this.config
        .get("/uni/getIsPush", {
          userId,
        })
        .then((res) => {
          console.log(res);
          if (res.data.data == 1) {
            this.isPtUserCode = true;
          } else {
            this.isPtUserCode = false;
          }
        });
    },
    getUiStyle() {
      var style = plus.navigator.getUiStyle();
      if ("dark" == style) {
        console.log("当前为暗黑模式");
        uni.setTabBarStyle({
          backgroundColor: "#1B1C1E",
        });
        uni.setNavigationBarColor({
          frontColor: "#ffffff",
          backgroundColor: "#1B1C1E",
          animation: {
            duration: 400,
            timingFunc: "easeIn",
          },
        });
      } else {
        console.log("当前为普通模式");
      }
    },
    //个人隐私开关
    change(e) {
      console.log(e);
      this.show = !e;
      if (e) {
        let flag = e ? 1 : 0;
        let device = uni.getStorageSync("device", 1);
        let cid = uni.getStorageSync("cid");
        let userId = uni.getStorageSync("user").id;
        console.log(device);
        this.config
          .post("/uni/isOpenPush", {
            isPush: flag,
            ptType: device,
            cid,
            userId,
          })
          .then((res) => {
            console.log(res);
          });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
span.tips {
  position: absolute;
  left: 6px;
  top: 22%;
  z-index: 1;
  font-size: 10px;
  zoom: 0.9;
  color: #ffffff;
}
.t1 {
  left: 28px !important;
  color: #000000 !important;
}
.banner {
  padding: 20rpx;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
</style>
<style scoped>
@media (prefers-color-scheme: closedark) {
  .bd {
    height: calc(100vh - var(--window-top));
    background-color: #1b1c1e !important;
    color: #f8f8f8 !important;
  }

  .b1 {
    background-color: #000000 !important;
    color: #f8f8f8 !important;
  }

  .b2 {
    color: #f8f8f8 !important;
  }
}
</style>